//
//  JYTUserManager.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 16/11/18.
//  Copyright © 2016年 com.yuan. All rights reserved.
//

#define kLoginSession @"session.id"
#define kLoginToken @"token.id"

#import "JYTUserManager.h"

@interface JYTUserManager()
{
    User *currentLoginUser;
    NSString *loginUserName;
    NSArray *cityAndServerUrlArr;
    NSArray *provinceArr;
}
@end

NSString *defaultSaveString = @"jyt.user.leaner";
NSString *userNameSaveString = @"jyt.user.loginName";

@implementation JYTUserManager

+ (JYTUserManager *) shared
{
    static dispatch_once_t onceToken;
    static JYTUserManager *userManager;
    dispatch_once(&onceToken, ^{
        userManager = [[JYTUserManager alloc]init];
    });
    
    return userManager;
}

//获取cityId
- (NSString *)getCityId
{
    NSString *cityId = nil;
    
    User *usr = [self getUser];
    
    cityId = usr.cityId;
    
    return cityId;
}

//用户角色 游客  轻量级用户  功能用户
- (NSString *)getRoleId
{
    NSString *roleId = nil;
    
    User * user = [self getUser];
    
    roleId = user.roleId;
    
    return roleId;
}


//设置url
- (void)setUrlToHttpClient:(JYTHttpClient *)client
{
    // NSString *roleId = [self getRoleId];
    
    NSString *cityId = [self getCityId];
    
    BOOL isOffline = [self isUsedOffLine];
    
    NSString *url = nil;
    
    if (isOffline) {
        url = @"http://183.62.167.18:9090/service/app/";
    } else {
        url = [self getUrlByCityId:cityId];
        url = [NSString stringWithFormat:@"%@app/", url];
    }
    
    //    client = [HttpClient sharedInstance];
    
    client.cityUrl = url;
    client.zoneUrl = url;
}
//获取url
- (NSString *)getUrlByCityId:(NSString *)cityId
{
    NSString *url = nil;
    
    if (!cityId) {
        return url;
    }
    
    JYTCity *city = [self getCityById:cityId];
    
    if (!city) {
        return url;
    }
    
    url = city.service_url;
    
    return url;
}

//获取指定城市
- (JYTCity *)getCityById:(NSString *)CityId
{
    JYTCity *rc = nil;
    
    if (!CityId || !provinceArr) {
        return rc;
    }
    
    
    for (JYTProvince *p in provinceArr) {
        
        NSArray *parray = p.cities;
        
        if (!parray) {
            continue;
        }
        
        for (JYTCity *city in parray) {
            
            if ([city.zone_id isEqual:[[NSNumber alloc] initWithInt:CityId.intValue]]) {
                rc = city;
                break;
            }
            
        }
        
    }
    
    return rc;
}



//获取是否登录
- (BOOL)isUsedOffLine
{
    BOOL isUsedOffline = YES;
    
    NSString *str =[self getUserDefaultValue:@"jyt.user.offline"];
    
    if ([str isEqualToString:@"0"]) {
        isUsedOffline = NO;
    } else {
        isUsedOffline = YES;
    }
    
    return isUsedOffline;
}
//获取字符串
- (NSString *) getUserDefaultValue:(NSString *)key
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    return  [ud stringForKey:key];
}
//保存登录状态 0 为登录 1 为没登录
- (void)setUsedOffLine:(NSString *)value
{
    if (value) {
        [self saveUserDefaultValue:value withKey:@"jyt.user.offline"];
    }
}
//保存字符串
- (void) saveUserDefaultValue:(NSString *) value withKey:(NSString *) key
{
    if (value && [value isKindOfClass:[NSString class]]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        [ud setObject:value forKey:key];
    }
}


//保存当前用户
- (void) setUser:(User *)user
{
    if (!user) {
        return;
    }
    
    currentLoginUser = user;
    
    NSDictionary *dic = user.mj_keyValues;
    
    if (dic) {
        [self saveUserDefaultDic:dic withKey:[self getUserkey:@"user"]];
    }
    
}
//获取当前用户
- (User *) getUser
{
    User *user = nil;
    if (currentLoginUser) {
        user = currentLoginUser;
    } else {
        NSDictionary *  dic = [self getUserDefaultDic:[self getUserkey:@"user"]];
        user = [User mj_objectWithKeyValues:dic];
        currentLoginUser = user;
    }
    
    return user;
    
}
//保存字典
- (void) saveUserDefaultDic:(NSDictionary *) value withKey:(NSString *) key
{
    if (value && [value isKindOfClass:[NSDictionary class]]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        [ud setObject:value forKey:key];
    }
}
//获取key 字符串
- (NSString *) getUserkey:(NSString *) key
{
    NSString *lu = [self getUserLoginName];
    
    if (lu == nil) {
        lu = @"jyt.user.save";
    }
    
    NSString *ss = [NSString stringWithFormat:@"%@.%@.%@", defaultSaveString,lu, key];
    return ss;
}
//获取字典
- (NSDictionary *) getUserDefaultDic:(NSString *)key
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    return  [ud objectForKey:key];
}


//保存登录名称
- (void) setUserLoginName:(NSString *)loginName
{
    loginUserName = loginName;
    [self saveUserDefaultValue:loginName withKey:userNameSaveString];
}
//获取登录名称
- (NSString *) getUserLoginName
{
    NSString *name = nil;
    if (loginUserName) {
        name = loginUserName;
    } else {
        name = [self getUserDefaultValue:userNameSaveString];
        loginUserName = name;
    }
    
    return name;
}


//保存昵称
- (void) setNikeName:(NSString *)name
{
    if (!name) {
        return;
    }
    
//    User *user = [self getUser];
//    
//    user.nickName = name;
//    
//    [self setUser:user];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:name forKey:@"nickname"];
    [defaults synchronize];
}
//获取昵称
- (NSString *) getNikeName
{
//    NSString *name = nil;
//    
//    User *user = [self getUser];
//    
//    name = user.nickName;
//    
//    return name;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:@"nickname"];
}




//保存用户头像
- (void) setUserHeadUrl:(NSString *)url
{
    if (!url) {
        return;
    }
    
//    User *user = [self getUser];
//    
//    user.headUrl = url;
//    
//    [self setUser:user];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:url forKey:@"url"];
    [defaults synchronize];
}

//获取用户头像
- (NSString *) getUserHeadUrl
{
//    NSString *hurl;
//    
//    
//    User *user = [self getUser];
//    
//    hurl = user.headUrl;
//    
//    return hurl;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:@"url"];
}


//保存省市区数组
- (void) setProviceArr:(NSArray *)parr andcsuArr:(NSArray *)csuArr
{
    if (parr) {
        provinceArr = parr;
    }
    
    if (csuArr) {
        cityAndServerUrlArr = csuArr;
    }

}
//获取省市区数组
- (NSArray *) getCsuArr
{
    return cityAndServerUrlArr;
}


//获取省数组
- (NSArray *) getProviceArr
{
    return provinceArr;
}

//获取市数组
- (NSArray *)getCityArr
{
    NSMutableArray *marr = [NSMutableArray array];
    
    NSArray *parr = [self getProviceArr];
    
    for (JYTProvince *pv in parr) {
        if (pv.cities) {
            [marr addObjectsFromArray:pv.cities];
        }
    }
    
    return marr;
}

//获取指定省份
- (JYTProvince *)getProvinceById:(NSString *)provinceId
{
    JYTProvince *province = nil;
    
    if (provinceId && provinceArr) {
        for (JYTProvince *p in provinceArr) {
            if ([p.zone_id isEqual:[[NSNumber alloc] initWithInt:provinceId.intValue]]) {
                province = p;
                break;
            }
        }
    }
    
    return province;
}

- (void) removeSaveInfomation
{
    currentLoginUser = nil;
    loginUserName = nil;
    cityAndServerUrlArr = nil;
    provinceArr = nil;
    
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud removeObjectForKey:userNameSaveString];
    
    [self setUsedOffLine:@"1"];
}



//保存数组
- (void) saveUserDefaultArray:(NSArray *) value withKey:(NSString *) key
{
    if (value && [value isKindOfClass:[NSArray class]]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        [ud setObject:value forKey:key];
    }
}
//获取数组
- (NSArray *) getUserDefaultArray:(NSString *)key
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    
    NSArray *array = [ud arrayForKey:key];
    
    return  array;
}



//获取获取data
- (NSData *) getUserDefaultData:(NSString *)key
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSData *data = [ud dataForKey:key];
    return  data;
}
//保存data
- (void) saveUserDefaultData:(NSData *) value withKey:(NSString *) key
{
    if (value && [value isKindOfClass:[NSData class]]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        [ud setObject:value forKey:key];
    }
}











@end
